import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermDocs;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.WildcardQuery;


public class LuceneTest {
	
	
	private static String FIELD_NAME = "content";
	private static String INDEX_DIR = "D:\\luceneIndex";
	
	
	public static void main(String[] args)throws Exception{
		Long startTime = new Date().getTime();
		StringBuffer candidateRest = new StringBuffer();
		Sorter sorter = new Sorter();
		RankMachine rankMachine = new RankMachine();
		int[] restList = new int[10];
		int restNum = 0;
		Analyzer analyzer = new StandardAnalyzer();
		//fuzzy query
//		Query query = new WildcardQuery(new Term("reviewContent", "the"));
		//multi-semantic query 
//		Query query1 = MultiFieldQueryParser.parse(new String[]{"popular", "xxxx"}, new String[]{"reviewContent", "restId"}, analyzer);
		
		//watch out for the uppercase
		PhraseQuery query = new PhraseQuery();
		query.add(new Term("reviewContent", "chinese"));
		query.add(new Term("reviewContent", "food"));
		query.setSlop(0);
		HashMap<String, Integer> restMap = new HashMap<String, Integer>();
		
		
		try{
			Searcher searcher = new IndexSearcher(INDEX_DIR);
			Hits hits = searcher.search(query);
			for(int i=0; i<hits.length(); i++){
//				System.out.println(hits.doc(i).get("restId")+" -- "+hits.doc(i).get("reviewId")+" -- "+hits.doc(i).get("reviewType"));
//				System.out.println(hits.score(i));
				String tempRest = hits.doc(i).get("restId");
				if (restMap.get(tempRest)==null){
					restMap.put(tempRest, 1);
				}else{
					int temp = restMap.get(tempRest);
					restMap.put(tempRest, temp+1);
				}
			}
			
			Iterator iter = restMap.keySet().iterator();
			System.out.println(restMap.size());
			int[] frequentRest = new int[restMap.size()+1];
			int i=0;
			Object key = null;
			while(iter.hasNext()){
				key = iter.next();
				frequentRest[i] = (Integer)restMap.get(key);
//				System.out.println((String)key+": "+ myTreeMap.get(key));
				i++;
			}
			sorter.quickSort(frequentRest, 0, restMap.size());
			//output the top20 restaurant which has the most frequent terms of the query
			for(int j=frequentRest.length-1; j>frequentRest.length-11; j--){
				if((frequentRest[j]>0)&&(frequentRest[j]!=frequentRest[j-1])){
		 			System.out.print(frequentRest[j]+" ");
					Iterator tempIter = restMap.keySet().iterator();
					Object tempKey = null;
					while(tempIter.hasNext()){
		//				System.out.println("it works");
						tempKey = tempIter.next();
						if(frequentRest[j]==(Integer)restMap.get(tempKey)){
							System.out.print((String)tempKey+" ");
							candidateRest.append((String)tempKey+" ");
							int score = rankMachine.getScore((String)tempKey);
							System.out.println(score);
						}
					}
				}else{
					continue;
				}
			}
			
			EmergingPattern ep = new EmergingPattern();
			String emergingResult = ep.getEmergingPattern(candidateRest.toString());
			String[] candidateRestTemp = candidateRest.toString().split(" ");
			Miner miner = new Miner(candidateRestTemp.length, ep.termNum);
			miner.computeNormTF(emergingResult);
			miner.computeTFR();
			miner.compileResult();
			
			// draw histogram here
			ChartTest chartTest = new ChartTest();
			//chartTest.drawChart(miner.wholeList, miner.getDocNum(), miner.getTermNum());
			
			Long endTime = new Date().getTime();
			System.out.println(emergingResult);
			System.out.println("runtime is: "+ (endTime-startTime));
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	
}
